import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import TableControls from '#cpt/temp/TableControls'
import Confirm from '#cpt/temp/Confirm'
// ===================================================================== global declare
const { $fn, $Lazy, $http } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
const LookModal				= $Lazy.state(()=>import('./cpt/look-modal'))
const statusData = [{ name:'待审核', value:'1' },{ name:'已审核', value:'2' }]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '账期',			name: 'summary_sn', 	type:'select', loading:true },
			{ label: '受检单位',		name: 'hosp_id', 		type:'select', loading:true },
			{ label: '业务员',		name: 'salesman_id', 	type:'select', loading:true },
			{ label: '状态',  		name: 'status', 		type:'select', data:statusData },
			{ label: '开票单位',		name: 'organization_name' },
			{ label: '操作时间',		names: ['start_at','end_at'], name:'time-null', type:'date-range' },
		]
	}
	
	model = {}
	power = {}
	
	componentDidMount(){
		const { type } = $fn.query()
		$http.submit(this,'fi-standing-book/checkPermission').then(data=>{
			this.power = data
		})
		if(type){ 
			this.type = type
		}
		this.getSelect()
		if(window.isInit){
			this.fetch()
		}
	}
	
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 账期
			$fn.setRow(forms, 'summary_sn', await f.getCache({ api:'bill-summary/select' } ) ) 
			// 业务员
			$fn.setRow(forms, 'salesman_id', await f.getCache({ api:'salesman' } ) )
			// 医院名称
			$fn.setRow(forms, 'hosp_id', await f.getCache({ api:'hospital' } ) )
			// 设置医院名称
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'fi-pay-book/index',{param:{doc_type:2}})
	
	// 表单
	cols = [
		{ title: '账期', 		dataIndex: 'summary_sn', 		width:100 },
		{ title: '受检单位', 	dataIndex: 'hosp_name', 		width:200 },
		{ title: '开票单位', 	dataIndex: 'organization_name', width:200 },
		{ title: '区域', 		dataIndex: 'region_name', 		width:80 },
		{ title: '业务员', 		dataIndex: 'salesman_user', 	...$fn.table.name },
		{ title: '单据类型', 	dataIndex: 'doc_type_name', 	width:100 },
		{ title: '账单金额', 	dataIndex: 'bill_price', 		...$fn.table.num },
		{ title: '调整日期', 	dataIndex: 'collection_date', 	...$fn.table.time },
		{ title: '调整金额', 	dataIndex: 'collection_price', 	...$fn.table.num },
		{ title: '未回款金额', 	dataIndex: 'not_collection_price', ...$fn.table.num },
		{ title: '制表人', 		dataIndex: 'add_user_name', 	...$fn.table.name },
		{ title: '审核人', 		dataIndex: 'audit_user_name', 	...$fn.table.name },
		{ title: '状态', 		dataIndex: 'status_name', 		...$fn.table.name },
		{ title: '制表时间', 	dataIndex: 'created_at', 		...$fn.table.time },
		{ title: '审核时间', 	dataIndex: 'audit_at', 			...$fn.table.time },
		{ title: '备注', 		dataIndex: 'remark', 			width:100 },
		TableControls.controls.call(this,{ width: 150 })
	]
	// 表格操作按钮
	table_controls = (text,{ hosp_id,uuid,summary_sn,status }) => {
		return [
			TableControls.click.call(this, '查看', ()=> this.lookModal.open({uuid,summary_sn,hosp_id,type:1}) ),
			+this.power.fipaybook_audit === 1 && +status === 1 ? Confirm.call(this, '审核', 'fi-pay-book/audit', { param: [uuid], idStr:'uuid' }):null
		]
	}
	// 操作按钮
	controls = () => {
		const { fipaybook_audit } = this.power
		return [
			+fipaybook_audit === 1 ? Confirm.call(this, '审核', 'fi-pay-book/audit', { idStr:'uuid', isKeys:true }) : null,
			this.type && Controls.back.call(this)	// 返回
		]
	}
	
	render(){
		return (
			<>
				<Page title='回款调整' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this,{onForm:()=>{
						this.searchFormRef.setValue({ hosp_id: this.hosp_id })
					}}) }
					{/* 表格 */}
					{ Table.call(this,{isInit:window.isInit}) }
				</Page>
				{/* 查看 */}
				<LookModal onRef={ref=>this.lookModal = ref} onRefresh={this.fetch}/>
			</>
		)
	}
}

export default Index